package com.exartisan.adapter;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.exartisan.bean.Category;
import com.exartisan.bean.ForumCategory;
import com.exartisan.thanhdoanhn.R;
import com.exartisan.thanhdoanhn.R.color;
import com.loopj.android.image.SmartImageView;

public class ForumCategoryAdapter extends ArrayAdapter<ForumCategory>
{
	Category cate;
	private Context ctx;
	ArrayList<ForumCategory> dataCategory;
	int row;
	
	public ForumCategoryAdapter(Context context,
			int textViewResourceId, ArrayList<ForumCategory> objects)
	{
		super(context, textViewResourceId, objects);
		this.ctx=context;
		this.dataCategory=objects;
		this.row=textViewResourceId;
				
	}

	

	@Override
	public int getCount()
	{
		return dataCategory.size();
	}

	

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ForumCategory catalogy=new ForumCategory();
		catalogy=dataCategory.get(position);
		View view;
		ViewHoder holder;
		view = convertView;
		LayoutInflater inflater = (LayoutInflater) ctx
			    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (convertView == null)
		{
			holder=new ViewHoder();
			/*inflater = LayoutInflater.from(context);
			view = inflater.inflate(R.layout.row_list_forum, null);*/
			   view = inflater.inflate(R.layout.row_list_forum, null);
			   holder.rowList4rum=(LinearLayout) view.findViewById(R.id.rowList4rum);
			   holder.imageView = (SmartImageView)view.findViewById(R.id.img_logo);
			   holder.textView = (TextView)view.findViewById(R.id.tv_title);
			    view.setTag(holder);
		
	  } else {
	   holder = (ViewHoder) view.getTag();
	  }
		
		//holder.imageView.setImageUrl(catalogy.getIcon());
		switch (position) {
		case 0:
			holder.imageView.setImageResource(R.drawable.amthuc);
			holder.rowList4rum.setBackgroundResource(R.drawable.row);
			break;
		case 1:
			holder.imageView.setImageResource(R.drawable.nhipsongtre);
			holder.rowList4rum.setBackgroundColor(Color.GRAY);
			break;

		default:
			holder.imageView.setImageResource(R.drawable.truyencuoi);
			holder.rowList4rum.setBackgroundResource(R.drawable.ro);
			break;
		}
		
		holder.textView.setText(catalogy.getTitle());
		

		return view;
	}
	class ViewHoder{
		 SmartImageView imageView;
		 TextView textView ;
		 LinearLayout rowList4rum;
	}
	//////////
	private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
		  SmartImageView bmImage;

		  public DownloadImageTask(SmartImageView bmImage) {
		      this.bmImage = bmImage;
		  }

		  protected Bitmap doInBackground(String... urls) {
		      String urldisplay = urls[0];
		      Bitmap mIcon11 = null;
		      try {
		        InputStream in = new java.net.URL(urldisplay).openStream();
		        mIcon11 = BitmapFactory.decodeStream(in);
		      } catch (Exception e) {
		          Log.e("Error", e.getMessage());
		          e.printStackTrace();
		      }
		      return mIcon11;
		  }

		  protected void onPostExecute(Bitmap result) {
		      bmImage.setImageBitmap(result);
		  }

}
}
